package br.com.fanor.job;

import br.com.fanor.manager.FactoryManager;
import br.com.fanor.manager.MessageManager;
import java.util.ArrayList;
import java.util.List;

public class MessageJob extends Thread {

    private static final long TIMER = 5000;
    private final MessageManager messageManager = FactoryManager.getMessageManager();
    private final List<String> messages = new ArrayList<String>();
    private int lastMessage = 0;
    
    {
        messages.add("Olá - 1");
        messages.add("Olá - 2");
        messages.add("Olá - 3");
        messages.add("Olá - 4");
        messages.add("Olá - 5");
        messages.add("Olá - 6");
        messages.add("Olá - 7");
        messages.add("Olá - 8");
        messages.add("Olá - 9");
        messages.add("Olá - 10");
    }
    
    @Override
    public void run() {
        while ( !isInterrupted() ) {
            messageManager.nofityListeners(getNextMessage());
            
            try {
                sleep(TIMER);
            } catch (InterruptedException ex) {}
        }
    }

    private String getNextMessage() {
        return messages.get(lastMessage == (messages.size()) ? lastMessage = 0 : lastMessage++);
    }
    
}
